﻿package com.towker.core.page;

import java.io.IOException;
import java.util.Enumeration;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 页面显示翻页的对象
 */
public class PageView
{
	private static long i = 0L;
	private final Log log = LogFactory.getLog(PageView.class);
	@SuppressWarnings("unchecked")
	private PageModel page;
	@SuppressWarnings("unchecked")
	private Enumeration params;
	private HttpServletRequest request;
	private boolean isOutForm = false;
	private String pageName = "page";
	private String pageSizeName = "pageSize";
	
	@SuppressWarnings("unchecked")
	public PageView(PageModel page, HttpServletRequest request) throws IOException
	{
		this.page = page;
		this.request = request;
		this.params = request.getParameterNames();
	}

	@SuppressWarnings("unchecked")
	public PageView(PageModel page, String pageName, String pageSizeName, HttpServletRequest request) throws IOException
	{
		this.page = page;
		this.request = request;
		this.params = request.getParameterNames();
		this.pageName = pageName;
		this.pageSizeName = pageSizeName;
	}

	private String getFormCode()
	{
		if (!isOutForm)
		{
			String pageFormId = "page" + String.valueOf(System.currentTimeMillis());
			String html = "<script language=\"javascript\">"
				+ "var __showPage__ = new ShowPageHelper({"
					+ "formId:\"" + pageFormId + "\""
					+ ", pageSize:" + page.getPageSize()
					+ ", totalPage:" + page.getTotalPage()
					+ ", pageName:\"" + pageName + "\""
					+ ", pageSizeName:\"" + pageSizeName + "\""
				+ "});</script>";
			html += "<form id=\"" + pageFormId + "\" action=\"\" method=\"post\" style=\"display:none;\">";
			html += "<input type=\"submit\" style=\"display:none;\" id=\"" + pageFormId + "_submitid\" />";
			if (params != null)
			{
				while (params.hasMoreElements())
				{
					String key = (String) params.nextElement();
					if (!key.equals(pageName) && !key.equals(pageSizeName))
					{
						String[] value = request.getParameterValues(key);
						if (value == null || value.length == 0)
						{
							html += "<input name=\"" + key + "\" type=\"hidden\" value=\"\"/> ";
						}
						else
						{
							for(int i = 0; i < value.length; i++)
							{
								if (value[i] == null) value[i] = "";
								html += "<input name=\"" + key + "\" type=\"hidden\" value=\"" + value[i].replace("\"", "&quot;") + "\"/> ";
							}
						}
					}
				}
			}
			html += "</form>";
			return html;
		}
		return "";
	}
	
	/**
	 * 输出表单
	 */
	public String getForm() {
		try
		{
			if (!isOutForm)
			{
				return getFormCode();
			}
		}
		catch (Exception e)
		{
			// TODO Auto-generated catch block
			log.error(e.toString());
		}
		return "";
	}

	/**
	 * 总页数信息
	 * @return
	 */
	private String getTotalRecord()
	{
		String re = "";
		re += "共" + page.getTotalCount() + "条记录&nbsp; ";
		return re;
	}

	/**
	 * 第几页信息
	 * @return
	 */
	private String getPageInfo()
	{
		String re = "";
		re += "第" + page.getCurrentPage() + "/" + page.getTotalPage() + "页 &nbsp;";
		return re;
	}

	/**
	 * 链接信息
	 * @return
	 */
	private String getPageLink()
	{
		String re = "";
		re += "<a" + ((page.getTotalPage() > 1 && page.getCurrentPage() > 1)?" onclick=\"__showPage__.goPage('1');return false;\" href=\"#\"":"") + ">首页</a>&nbsp;";
		re += "<a" + ((page.isHasPreviousPage())?" onclick=\"__showPage__.goPage('" + page.getPreviousPage() + "');return false;\" href=\"#\"":"") + ">上页</a>&nbsp;";
		re += "<a" + ((page.isHasNextPage())?" onclick=\"__showPage__.goPage('" + page.getNextPage() + "');return false;\" href=\"#\"":"") + ">下页</a>&nbsp;";
		re += "<a" + ((page.getTotalPage() > 1 && page.getCurrentPage() < page.getTotalPage())?" onclick=\"__showPage__.goPage('" + page.getTotalPage() + "');return false;\" href=\"#\"":"") + ">尾页</a>&nbsp;";
		return re;
	}

	/**
	 * 显示跳转信息
	 * @return
	 */
	private String getJumpPage()
	{
		String re = "";
		i++;
		if (i > 100L)
			i = 0L;
		String pId = "page" + i;
		re += "转到第 <input type=\"text\" class=\"input\" id=\"" + pId + "\" /> 页 "
				+ "<input type=\"button\" class=\"go\" value=\"GO\" onclick=\"__showPage__.goToPage('" + pId + "')\" />";
		return re;
	}
	
	/**
	 * 显示分页控件
	 * @param isViewTotal 是否显示所有记录数
	 * @param isViewPageInfo 是否显示页面信息
	 * @param isShowLink 是否翻页
	 * @param isShowJump 是否支持跳转
	 */
	public String getLink(boolean isViewTotal, boolean isViewPageInfo, boolean isShowLink, boolean isShowJump)
	{
		String html = "";
		if (isViewTotal)
			html += getTotalRecord();
		if (isViewPageInfo)
			html += getPageInfo();
		if (isShowLink)
			html += getPageLink();
		if (isShowJump)
			html += getJumpPage();
		return html;
	}
	
	/**
	 * 显示翻页效果
	 */
	public String getPage()
	{
		return getForm() + getLink(true, true, true, true);
	}
	
	/**
	 * 输出表单
	 */
	public String showForm() {
		return getForm();
	}
	
	/**
	 * 显示默认的翻页效果
	 */
	public String showPage()
	{
		return showPage(true, true, true, true);
	}

	/**
	 * 显示分页控件
	 */
	public String showPage(boolean isViewTotal, boolean isViewPageInfo, boolean isShowLink, boolean isShowJump)
	{
		try
		{
			return getForm() + getLink(isViewTotal, isViewPageInfo, isShowLink, isShowJump);
		}
		catch (Exception e)
		{
			// TODO Auto-generated catch block
			log.error(e.toString());
		}
		return "";
	}
}
